<%@ page import="org.apache.poi.ss.usermodel.*" %>
<%@ page import="org.apache.poi.xssf.usermodel.XSSFWorkbook" %>
<%@ page contentType="application/vnd.ms-excel;charset=UTF-8" language="java" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="dao.AttendanceDao" %>
<%@ page import="vo.Attendance" %>
<%@ page import="java.util.List" %>
<%@ page import="dao.StudentDao" %>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook" %>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFRichTextString" %>
<!DOCTYPE html>
<html>
    <head>
        <title>main</title>
    </head>
    <body>
<%
    response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename=excel.xls"+";filename*=utf-8''excel.xls");
    Workbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("Sheet1");
    Row titleRow = sheet.createRow(0);
    titleRow.createCell(0).setCellValue(new HSSFRichTextString("姓名"));
    titleRow.createCell(1).setCellValue(new HSSFRichTextString("学号"));
    titleRow.createCell(2).setCellValue(new HSSFRichTextString("出勤情况"));
    try {
            String attendanceTime = request.getParameter("attendance_time");
            String teacherId = request.getParameter("teacher_id");
            AttendanceDao attendanceDao = new AttendanceDao();
            List<Attendance> attendances = new ArrayList<>();
            StudentDao studentDao = new StudentDao();
            int rowNum = 1;
            attendances = attendanceDao.getAttendanceByTimeAndTeacherId(attendanceTime, teacherId);
            for(Attendance c : attendances){
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(studentDao.findWithId(c.getStu_id()).getName());
            row.createCell(1).setCellValue(c.getStu_id());
            row.createCell(2).setCellValue(c.getStatus().toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    OutputStream output = response.getOutputStream();
    workbook.write(output);
    output.close();
%>
</body>
</html>

